//@dart=2.12
import 'dart:ui';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/code_value_entity.dart';
import 'package:ls_work/bad_event/response/bad_event_list_data.dart';
import 'package:saas_order_meal/model/combo_model.dart';
import 'package:saas_order_meal/net/net_response/code_value_list.dart';

import '../../res/colors.dart';
import '../bad_event_model.dart';
import '../ui/elder_fall_event_page.dart';

class BadEventListViewModel extends BaseListViewModel<BadEventListData> {

  BadEventListViewModel(BuildContext context) : super(context);

  var commonModel = ComboModel();

  var eventModel = BadEventModel();

  List<CodeValueList> eventTypeList = [];

  CodeValueList? eventType;

  String? searchName;

  List<PickerDataWithIdModel> statusList = [
    PickerDataWithIdModel(pickId: '0', pickName: '待归档'),
    PickerDataWithIdModel(pickId: '1', pickName: '已归档'),
    PickerDataWithIdModel(pickId: '2', pickName: '已作废'),
  ];

  PickerDataWithIdModel? status;

  @override
  Future<List<BadEventListData>> requestListData() async {
    if (eventTypeList.isEmpty) {
      eventTypeList = await commonModel.getComboData("58");
    }
    return eventModel.badEventList(page: page,
        rows: pageLength,
        elderNameLike: searchName,
        createDateStart: startDateStr,
        createDateEnd: startDateStr,
        eventType: eventType?.codeValue,
        archivingStatus:status?.pickId);
  }

  Future<void> chooseEventType() async {
    if (eventTypeList.isEmpty) {
      try{
        eventTypeList = await commonModel.getComboData("58");
      }catch(E){

      }
    }
    DataPickerView(context,eventTypeList,currentFirstData: eventType,onSubmitListener: (p1,p2,p3){
      eventType = eventTypeList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void chooseStatus(){
    DataPickerView(context,statusList,currentFirstData: status,onSubmitListener: (p1,p2,p3){
      status = statusList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  Color statusColor(num status){
    if(status==0){
      return Color(0xffEAAE0B);
    }
    if(status==1){
      return Color(0xff999999);
    }
    
    return Color(0xffDA1919);
  }

  void resetClick() {
    eventType = null;
    searchName = null;
    status = null;
    startDateStr = "";
    startDateTime = null;
    notifyListeners();
    requestRefresh();
  }

  void addClick(){
    showDialog(context: context, builder: (context){
      CodeValueList? chooseType;
      return Dialog(
        elevation: 0,
        backgroundColor: Colors.transparent,
        child: StatefulBuilder(builder: (context,setState){
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(height: 16,),
              CommonText('事件类型',textColor: Color(0xff333333),fontWeight: FontWeight.w500),
              SizedBox(height: 11,),
              ...(eventTypeList).map((e) {
                return Row(
                  children: [
                    Icon(chooseType==e? Icons.check_circle:Icons.radio_button_off,color: Color(0xff159BD5),size: 16,),
                    SizedBox(width: 18,),
                    CommonText(e.nameCn,textColor: Color(0xff333333),fontWeight: FontWeight.w500)
                  ],
                ).onTap(() {
                  setState((){
                    chooseType = e;
                  });
                });
              }).toList().insertWidget(SizedBox(height: 16,)),

              SizedBox(height: 20,),

              Row(
                children: [
                  TextButton(
                    onPressed: () {
                      Go().pop();
                    },
                    child: CommonText('取消', textColor: Color(0xff333333),fontWeight: FontWeight.w500),
                    style: ButtonStyle(
                        shape: MaterialStateProperty.all(RoundedRectangleBorder(
                            borderRadius: 6.borderRadius,
                            side: BorderSide(color: Colours.gray_99)))),
                  ).setWeight(1),
                  SizedBox(width: 12,),
                  TextButton(
                      onPressed: () async {
                        if(chooseType==null){
                          showToast("请选择类型");
                          return;
                        }
                        await Go().pushAndPop(ElderFallEventPage(eventType: num.tryParse(chooseType!.codeValue!),));
                        requestRefresh();
                      },
                      child: CommonText('确定', textColor: Colors.white),
                      style: ButtonStyle(
                          backgroundColor: MaterialStateProperty.all(Color(0xff159BD5)),
                          shape: MaterialStateProperty.all(RoundedRectangleBorder(
                            borderRadius: 6.borderRadius,
                            side: BorderSide(color: Colors.blue),
                          )))).setWeight(1),
                ],
              ),
              SizedBox(height: 16,)
            ],
          );
        },).addToContainer(
          padding: EdgeInsets.symmetric(horizontal: 24),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: 8.borderRadius
            )
        ),
      );
    });
  }
}